﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Data;
using System.Web.UI.WebControls;
using weikebuluo.Model;
using weikebuluo.DAL;

namespace weikeweixin.web.admin.wechat
{
    public partial class menuadd : weikeweixin.web.admin.cs.adminbase
    {
        w_caidan_dal caidandal = new w_caidan_dal();
        protected w_caidan_model caidanmodel = new w_caidan_model();

        w_article_dal articledal = new w_article_dal();
        protected DataTable dtarticle = new DataTable();

        protected void Page_Load(object sender, EventArgs e)
        {
            dtarticle = articledal.GetList("useer_id=" + user_id + " and article_layid=0").Tables[0];

            if (!string.IsNullOrEmpty(Request.QueryString["id"])) {
                caidanmodel = caidandal.GetModel(int.Parse(Request.QueryString["id"]));

            }

            switch (Request.Form["action"]) { 
                case "add":
                    add();
                    break;
            }
        }

        //新增
        private void add()
        {
            if (!string.IsNullOrEmpty(Request.Form["id"]))
            {
                caidanmodel = caidandal.GetModel(int.Parse(Request.Form["id"]));
            }
            if (!string.IsNullOrEmpty(Request.Form["layid"]))
            {
                caidanmodel.caidan_layid = int.Parse(Request.Form["layid"]);
            }
            else {
                caidanmodel.caidan_layid = 0;
            } 
            caidanmodel.caidan_name = Request.Form["caidan_name"].Trim().ToString();
            caidanmodel.caidan_type = Request.Form["caidan_type"].Trim().ToString();
            caidanmodel.caidan_order = int.Parse(Request.Form["caidan_order"].Trim().ToString());
            caidanmodel.wechat_id = int.Parse(wechat_id);

            switch (Request.Form["caidan_type"])
            {
                case "view":
                    caidanmodel.caidan_url = Request.Form["caidan_url"].Trim().ToString();
                    break;
                case "click":
                    caidanmodel.article_id = int.Parse(Request.Form["article_id"].Trim().ToString());
                    caidanmodel.caidan_retype = "news";
                    break;
            }
            caidanmodel.wechat_id = int.Parse(wechat_id);



            if (!string.IsNullOrEmpty(Request.Form["layid"]))
            {
                if (!string.IsNullOrEmpty(Request.Form["id"]))
                {
                    bool re = caidandal.Update(caidanmodel);
                    if (re)
                    {
                        Response.Write("{\"errno\":\"0\",\"tip\":\"保存成功！\",\"url\":\"menulist.aspx?layid=" + Request.Form["layid"] + "\",\"error\":\"\"}");
                        Response.End();
                    }
                    else
                    {
                        Response.Write("{\"errno\":\"0\",\"tip\":\"保存失败！\",\"url\":\"menuadd.aspx?layid=" + Request.Form["layid"] + "\",\"error\":\"\"}");
                        Response.End();
                    }
                }
                else
                {
                    caidanmodel.caidan_addtime = DateTime.Now;
                    caidanmodel.caidan_key = Guid.NewGuid().ToString();

                    int re = caidandal.Add(caidanmodel);
                    if (re > 0)
                    {
                        Response.Write("{\"errno\":\"0\",\"tip\":\"保存成功！\",\"url\":\"menulist.aspx?layid=" + Request.Form["layid"] + "\",\"error\":\"\"}");
                        Response.End();
                    }
                    else
                    {
                        Response.Write("{\"errno\":\"0\",\"tip\":\"保存失败！\",\"url\":\"menuadd.aspx?layid=" + Request.Form["layid"] + "\",\"error\":\"\"}");
                        Response.End();
                    }
                }
            }
            else
            {
                if (!string.IsNullOrEmpty(Request.Form["id"]))
                {
                    bool re = caidandal.Update(caidanmodel);
                    if (re)
                    {
                        Response.Write("{\"errno\":\"0\",\"tip\":\"保存成功！\",\"url\":\"menulist.aspx\",\"error\":\"\"}");
                        Response.End();
                    }
                    else
                    {
                        Response.Write("{\"errno\":\"0\",\"tip\":\"保存失败！\",\"url\":\"menuadd.aspx\",\"error\":\"\"}");
                        Response.End();
                    }
                }
                else
                {
                    caidanmodel.caidan_addtime = DateTime.Now;
                    caidanmodel.caidan_key = Guid.NewGuid().ToString();

                    int re = caidandal.Add(caidanmodel);
                    if (re > 0)
                    {
                        Response.Write("{\"errno\":\"0\",\"tip\":\"保存成功！\",\"url\":\"menulist.aspx\",\"error\":\"\"}");
                        Response.End();
                    }
                    else
                    {
                        Response.Write("{\"errno\":\"0\",\"tip\":\"保存失败！\",\"url\":\"menuadd.aspx\",\"error\":\"\"}");
                        Response.End();
                    }
                }
            }
        }
    }
}